home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / SPIM Folder / Sources / macmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-08  |  3.2 KB  |  118 lines  |  [TEXT/KAHL]

  1. /* SPIM S20 MIPS simulator.
  2.    Copyright (C) 1990 by James Larus (larus@cs.wisc.edu).
  3.  
  4.    Macintosh Version by Philip Delaquess (delaques@gcg.com)
  5.    Copyright (C) 1993 by Saunders College Publishing and Morgan Kaufman Publishers.
  6.  
  7.    SPIM is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by the
  9.    Free Software Foundation; either version 1, or (at your option) any
  10.    later version.
  11.  
  12.    SPIM is distributed in the hope that it will be useful, but WITHOUT
  13.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15.    for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with GNU CC; see the file COPYING.  If not, write to James R.
  19.    Larus, Computer Sciences Department, University of Wisconsin--Madison,
  20.    1210 West Dayton Street, Madison, WI 53706, USA or to the Free
  21.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. Problems: 1) SPIM was designed for a huge virtual memory machine, so it mallocs
  24. ad hoc and doesn't have to free rigourously.  2) The malloc and free routines with
  25. THINK C are sorta flaky anyway. Loading a huge program, clearing everything, then
  26. loading the same program again runs out of memory and crashes nastily. Not all
  27. the mallocs are verified.
  28.  
  29. Solution: Ask the Mac OS how much memory there is. Allocate a relocatable block of
  30. about 90% of that (after building most of the interface), move it high, and lock
  31. it. Malloc requests are satisfied from this giant block, with no housekeeping.
  32. Free requests are simply ignored. When the user wants to Clear All, we just reset
  33. the pointer to the beginning of our huge block and start again.
  34.  
  35. */
  36.  
  37. #define NUM 8
  38. #define DEN 10 /* we grab NUM/DEN of the total available memory */
  39.  
  40. static Handle MacMemory;
  41. static Ptr NextAvailable;
  42. static Size MemorySize, AmountRemaining;
  43.  
  44. /* CreateMacMemory
  45. *
  46. *  called once at initialization, it allocates a huge relocatable block and locks it.
  47. */
  48.  
  49. void CreateMacMemory()
  50. {
  51.     Size grow, maximum;
  52.  
  53.     maximum = MaxMem(&grow);
  54.     AmountRemaining = MemorySize = (maximum * NUM) / DEN;
  55.     MacMemory = NewHandle(MemorySize);
  56.     HLock(MacMemory);
  57.     NextAvailable = *MacMemory;
  58. } /* CreateMacMemory */
  59.  
  60. /* ResetMacMemory
  61. *
  62. *  resets the memory pointer to the beginning of the block.
  63. */
  64.  
  65. void ResetMacMemory()
  66. {
  67.     AmountRemaining = MemorySize;
  68.     NextAvailable = *MacMemory;
  69. } /* ResetMacMemory */
  70.  
  71. /* FreeMacMemory
  72. *
  73. *  gives the big block back to the OS.
  74. */
  75.  
  76. void FreeMacMemory()
  77. {
  78.     HUnlock(MacMemory);
  79.     DisposHandle(MacMemory);
  80. } /* FreeMacMemory */
  81.  
  82. /* malloc
  83. *
  84. *  is our treacherous friend from the standard library. This version just grabs the
  85. *  next several bytes from the big block.
  86. */
  87.  
  88. void *malloc(Size size)
  89. {
  90.     void *temp;
  91.  
  92.     if ( size > AmountRemaining )
  93.         return (void *) 0;
  94.     temp = NextAvailable;
  95.     NextAvailable += size;
  96.     AmountRemaining -= size;
  97.  
  98.     /* force even */
  99.     if ( (long) NextAvailable & 0x01 ) {
  100.         ++NextAvailable;
  101.         --AmountRemaining;
  102.     }
  103.     return temp;
  104. } /* malloc */
  105.  
  106. /* realloc and free
  107. *
  108. *  are do-nothing stubs.
  109. */
  110.  
  111. void *realloc(void *ptr, Size size)
  112. {
  113. } /* realloc */
  114.  
  115. void free(void *ptr)
  116. {
  117. } /* free */
  118.